<?php

/**
 * 就业方向控制器 后台处理数据
 * @date 2014/01/11
 * @author 马燕龙
 */
class EmployAction extends CommonAction
{

    public function index2()
    {
        $list = D('Employ')->relation(true)->select();
        var_dump($list);
    }


    //数据显示及搜索
    public function index()
    {

        //处理搜索条件，组装成$map数组
        $map = array();
        $name = I('name');
        if (!empty($name)) {
            $map['name'] = $name;
        }
        $grade_id = I('grade_id');
        if (!empty($grade_id)) {
            $map['grade_id'] = $grade_id;
        }

        $Employ = D('Employ');
        $count = $Employ->where($map)->count();

        import('ORG.Util.Page');
        $Page = new Page($count, 20);
        $show = $Page->show();

        $list = $Employ->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        //处理数据 将对应的状态改为对应的值
        $count = count($list);
        for ($i = 0; $i < $count; $i++) {
            $list[$i]['grade_id'] = D('Grade')->getGradeName($list[$i]['grade_id']);
            $list[$i]['area_id'] = D('Area')->getAreaName($list[$i]['area_id']);
            $list[$i]['education'] = $Employ->education[$list[$i]['education']];
            $list[$i]['method'] = $Employ->method[$list[$i]['method']];
            $list[$i]['position'] = $Employ->position[$list[$i]['position']];
            $list[$i]['pay'] = $Employ->pay[$list[$i]['pay']];
            $list[$i]['phpjunior'] = $Employ->leval[$list[$i]['phpjunior']];
            $list[$i]['phpsenior'] = $Employ->leval[$list[$i]['phpsenior']];
            $list[$i]['mysql'] = $Employ->leval[$list[$i]['mysql']];
            $list[$i]['js'] = $Employ->leval[$list[$i]['js']];
            $list[$i]['linux'] = $Employ->leval[$list[$i]['linux']];
        }

        $gradeList = D('Grade')->getGradeList();
        $this->assign('gradeList', $gradeList);    //班级数组 用于搜索
        $this->assign('map', $map);    //条件数组 用于显示搜索条件
        $this->assign('list', $list);    //数据列表
        $this->assign('page', $show);    //分页
        $this->display();
    }

    //导出数据到Excel表格
    public function exportData()
    {

        //获取相应数据
        $Employ = D('Employ');
        $type = I('type', 1);
        $typeData = $Employ->type[$type];
        $map['grade_id'] = I('grade_id');
        $list = $Employ->where($map)->select();

        //如果没有数据，则返回
        if (empty($list)) {
            $this->error('暂无数据！', U('Grade/index'));
            exit;
        }
//        print_r($list);exit;

        //处理数据 将对应的状态改为对应的值
        $count = count($list);
        for ($i = 0; $i < $count; $i++) {
            $list[$i]['grade_id'] = D('Grade')->getGradeName($list[$i]['grade_id']);
            $list[$i]['area_id'] = D('Area')->getAreaName($list[$i]['area_id']);
            $list[$i]['education'] = $Employ->education[$list[$i]['education']];
            $list[$i]['method'] = $Employ->method[$list[$i]['method']];
            $list[$i]['position'] = $Employ->position[$list[$i]['position']];
            $list[$i]['pay'] = $Employ->pay[$list[$i]['pay']];
            foreach ($typeData as $key => $val) {
                $list[$i][$val['name']] = $Employ->leval[$list[$i][$val['name']]];
            }
        }

        //导入PHPExcel扩展类
        vendor('PHPExcel.PHPExcel');
        vendor('PHPExcel.PHPExcel.IOFactory');
        vendor('PHPExcel.PHPExcel.Reader.Excel5');

        $excel = new PHPExcel();    //实例化新工作薄对象
        $sheet = $excel->getActiveSheet();    //获取活动工作表

        //标题
        $title = array(
            '编号', '班级', '姓名','性别', '年龄','手机号码', '籍贯', '是否统招', '身份证号码', '家庭住址', '家庭号码', '学历','毕业时间', '毕业院校', '院校所在省', '专业',
            '就业方式', '就业地域', '就业职位', '期望薪资','qq号码',
        );
        foreach ($typeData as $key => $val) {
            $title[] = $val['display'];
        }

        //循环出标题到表格
        $count = count($title);
        for ($i = 0; $i < $count; $i++) {
            $sheet->setCellValueByColumnAndRow($i, 1, $title[$i]);    //第0列的第1行
        }

        //循环输出数据到表格
        $count = count($list);
        for ($i = 0; $i < $count; $i++) {
            $sheet->setCellValueByColumnAndRow(0, $i + 2, $list[$i]['id']);    //第0列的第1行
            $sheet->setCellValueByColumnAndRow(1, $i + 2, $list[$i]['grade_id']);
            $sheet->setCellValueByColumnAndRow(2, $i + 2, $list[$i]['name']);

            if ( $list[$i]['sex'] == 1)
            {
                $list[$i]['sex'] = '男';
            }else{
                $list[$i]['sex'] = '女';
            }
            $sheet->setCellValueByColumnAndRow(3, $i + 2, $list[$i]['sex']);

            $sheet->setCellValueByColumnAndRow(4, $i + 2, $list[$i]['age']);
            $sheet->setCellValueByColumnAndRow(5, $i + 2, $list[$i]['phone'].' ');

            $sheet->setCellValueByColumnAndRow(6, $i + 2, $list[$i]['address']);
            // 波波
            // 2017-11-29
            // 增加是否统招
            // 身份证号码
            // 家庭住址
            // 家庭电话
            if ( $list[$i]['is_tongzhao'] == 1)
            {
                $list[$i]['is_tongzhao'] = '是';
            }else{
                $list[$i]['is_tongzhao'] = '否';
            }
            $sheet->setCellValueByColumnAndRow(7, $i + 2, $list[$i]['is_tongzhao']);
            $sheet->setCellValueByColumnAndRow(8, $i + 2, $list[$i]['id_card'].' ');
            $sheet->setCellValueByColumnAndRow(9, $i + 2, $list[$i]['home_address']);
            $sheet->setCellValueByColumnAndRow(10, $i + 2, $list[$i]['home_tel']);



            $sheet->setCellValueByColumnAndRow(11, $i + 2, $list[$i]['education']);
            $sheet->setCellValueByColumnAndRow(12, $i + 2, $list[$i]['graduate_year'].'-'.$list[$i]['graduate_month']);
            $sheet->setCellValueByColumnAndRow(13, $i + 2, $list[$i]['graduate']);


            $sheet->setCellValueByColumnAndRow(14, $i + 2, $list[$i]['graduateadd']);
            $sheet->setCellValueByColumnAndRow(15, $i + 2, $list[$i]['subject']);
            $sheet->setCellValueByColumnAndRow(16, $i + 2, $list[$i]['method']);

            $sheet->setCellValueByColumnAndRow(17, $i + 2, $list[$i]['area_id']);
            $sheet->setCellValueByColumnAndRow(18, $i + 2, $list[$i]['position']);
            $sheet->setCellValueByColumnAndRow(19, $i + 2, $list[$i]['pay']);
            $sheet->setCellValueByColumnAndRow(20, $i + 2, $list[$i]['qq'].' ');


            foreach ($typeData as $key => $val) {
                $sheet->setCellValueByColumnAndRow(21 + $key, $i + 2, $list[$i][$val['name']]);
//                print_r($list[$i][$val['name']]);
            }
//            exit;
        }

        //临时文件
        $file = date('Y-m-d-H-i-s', time()) . '.xls';
        $write = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
        $write->save($file);
        //弹出下载对话框
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($file));
        header('Content-Length: ' . filesize($file));
        readfile($file);
        //删除临时文件
        unlink($file);

    }

    /**
     * 个人就业信息详情
     */
    public function ajaxDetail()
    {

        //查找对应记录
        $id = I('id');
        $Employ = D('Employ');
        $data = $Employ->find($id);
        //替换对应的状态
        $data['grade_id'] = D('Grade')->getGradeName($data['grade_id']);
        $data['area_id'] = D('Area')->getAreaName($data['area_id']);
        $data['education'] = $Employ->education[$data['education']];
        $data['method'] = $Employ->method[$data['method']];
        $data['position'] = $Employ->position[$data['position']];
        $data['pay'] = $Employ->pay[$data['pay']];
        $data['phpjunior'] = $Employ->leval[$data['phpjunior']];
        $data['phpsenior'] = $Employ->leval[$data['phpsenior']];
        $data['mysql'] = $Employ->leval[$data['mysql']];
        $data['js'] = $Employ->leval[$data['js']];
        $data['linux'] = $Employ->leval[$data['linux']];

        $this->assign('item', $data);
        $this->display('detail');
    }


}